package com.itheima.controller.admin;

import com.itheima.annotation.PreAuthorize;
import com.itheima.dto.EmployeeDTO;
import com.itheima.dto.EmployeeLoginDTO;
import com.itheima.dto.EmployeePageQueryDTO;
import com.itheima.dto.PasswordEditDTO;
import com.itheima.result.PageBean;
import com.itheima.result.Result;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;

import com.itheima.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/employee")
@Api(tags = "员工相关接口")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */

    @PostMapping("/login")
    public Result login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        EmployeeLoginVO employeeLoginVO = employeeService.EmployeeLogin(employeeLoginDTO);
        return Result.success(employeeLoginVO);
    }

    /**
     * 退出登录
     *
     * @return
     */
    @PostMapping("/logout")
    public Result logout() {
        employeeService.logout();
        return Result.success();
    }

    /**
     * 修改密码
     *
     * @param passwordEditDTO
     * @return
     */

    @PutMapping("/editPassword")
    public Result Revise(@RequestBody PasswordEditDTO passwordEditDTO) {
        System.out.println(passwordEditDTO + "============================");
        employeeService.updatePassword(passwordEditDTO);
        return Result.success();

    }

    /**
     * 分页查询
     *
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result page(EmployeePageQueryDTO employeePageQueryDTO) {
        PageBean pageBean = employeeService.page(employeePageQueryDTO);
        return Result.success(pageBean);
    }

    /**
     * 添加员工
     *
     * @param employeeDTO
     * @return
     */
    @PostMapping
    public Result addEmployee(@RequestBody EmployeeDTO employeeDTO) {
        System.out.println(employeeDTO + "=========================");
        employeeService.add(employeeDTO);
        return Result.success();
    }

    /**
     * 启用禁用员工账号
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @PreAuthorize(value = "admin:employee:status")
    public Result updateStatus(@PathVariable Integer status, Long id) {
        employeeService.updateStatus(status, id);
        return Result.success();

    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result selectById(@PathVariable Integer id) {
        Employee employee = employeeService.selectById(id);
        return Result.success(employee);
    }

    /**
     * 修改员工信息
     * @param employeeDTO
     * @return
     */

    @PutMapping
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        employeeService.update(employeeDTO);
        return Result.success();
    }
}
